package com.yunding.config;

import com.yunding.constants.SystemConstants;
import com.yunding.enums.ResultStatuesEnums;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  Swagger2
 * </p>
 *
 * @author 郭思良
 * @create 2020-07-23 15:38
 */


@Configuration
@EnableSwagger2
public class SwaggerConfig {

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Java一组Dresults云笔记 API 文档")
                .description("Java一组Dresults云笔记 API 文档\n" + "所有返回码如下\n" + ResultStatuesEnums.toPrettyString())
                .version("2.0.1")
                .build();
    }

    @Bean
    public Docket createRestApi() {

        // 在请求参数的header中添加token参数
        ParameterBuilder tokenParam1 = new ParameterBuilder();
        ParameterBuilder tokenParam2 = new ParameterBuilder();
        List<Parameter> parameters = new ArrayList<>();
        tokenParam1.name(SystemConstants.TOKEN)
                .description("token")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .build();
        parameters.add(tokenParam1.build());

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .globalOperationParameters(parameters)
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                .paths(PathSelectors.any())
                .build();
    }
}

